﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JEng_Mark_1.SceneObject;
using Microsoft.Xna.Framework.Graphics;
using JEng_Mark_1.Managers;
using Microsoft.Xna.Framework;
using JEng_Mark_1.Interfaces;

namespace JEng_Mark_1.StandardObjects
{
    public class JGridObject : JSceneObject, IJEngLoadable
    {
        private Color _lineColor;
        private float _lineSpacing;
        private int _gridSize;
        private VertexBuffer _vb;
        private int _numberVertices;

        private BasicEffect _effect;

        public JGridObject(Color lineColor, int gridSize, float lineSpacing)
        {
            _gridSize = gridSize;
            _lineColor = lineColor;
            _lineSpacing = lineSpacing;
            _numberVertices = _gridSize * 4;
        }

        public void LoadContent()
        {
            _effect = new BasicEffect(JEngineManager.Device, null);

            //create buffers
            CreateVB();

            ReadyToRender = true;
        }

        private void CreateVB()
        {
            _vb = new VertexBuffer(JEngineManager.Device, typeof(VertexPositionColor), _numberVertices, BufferUsage.WriteOnly);

            VertexPositionColor[] vertices = new VertexPositionColor[_numberVertices];

            float maxEdge = _gridSize * _lineSpacing;

            //calculate vertices
            for (int i = 0; i < _gridSize; i++)
            {
                Vector3 p = new Vector3(0, 0, (i * _lineSpacing));
                vertices[(i*2)] = new VertexPositionColor(p, _lineColor);

                p = new Vector3(maxEdge, 0, (i * _lineSpacing));
                vertices[(i*2) + 1] = new VertexPositionColor(p, _lineColor);
            }
            for (int i = 0; i < _gridSize; i++)
            {
                Vector3 p = new Vector3((i * _lineSpacing),0,0);
                vertices[(i*2) + (_gridSize*2)] = new VertexPositionColor(p, _lineColor);

                p = new Vector3((i * _lineSpacing), 0, maxEdge);
                vertices[(i*2) + 1 + (_gridSize*2)] = new VertexPositionColor(p,_lineColor);
            }
            _vb.SetData<VertexPositionColor>(vertices);
        }

        public override void Draw(GameTime gameTime)
        {
            _effect.World = World;
            _effect.View = JCameraManager.ActiveCamera.View;
            _effect.Projection = JCameraManager.ActiveCamera.Projection;

            _effect.DiffuseColor = _lineColor.ToVector3();

            _effect.Begin();
            foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
            {
                pass.Begin();
                using (VertexDeclaration declaration = new VertexDeclaration(JEngineManager.Device, VertexPositionColor.VertexElements))
                {
                    JEngineManager.Device.VertexDeclaration = declaration;
                    JEngineManager.Device.Vertices[0].SetSource(_vb, 0, VertexPositionColor.SizeInBytes);
                    for (int i = 0; i < _gridSize*2; i++)
                    {
                        JEngineManager.Device.DrawPrimitives(PrimitiveType.LineList, i * 2, 1);
                    }
                    //JEngineManager.Device.DrawIndexedPrimitives(PrimitiveType.LineList,
                       // 0, 0, _numberVertices, 0, _gridSize * 2);
                }
                pass.End();
            }
            _effect.End();
        }
        
        public void UnloadContent()
        {
           
        }
    }
}
